<template>
  <div></div>
</template>

<script>
export default {
  name: 'MySocket',
  sockets: {
    connect: function () {
      console.log('socket 连接成功!')
      if (typeof this.CurrUser.IsLogin === 'function' && this.CurrUser.IsLogin()) {
        this.sendMessage({})
      }
      // this.sendMessage({})
    },
    // customEmit: function () {
    //   console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
    // }
  },
  mounted () {
    let $this=this
    $this.sockets.subscribe('newSocketIO-ServerMessage', (data) => {
      if (data.eventType==='LOGIN'){
        if (data.message==='ERROR'){
          this.successTip('socket登录失败,退出登录状态','error')
          // this.CurrUser.setCache(null)
        }else{
          console.log('socket登录成功')
          // this.successTip('socket登录成功')
        }
      }

      if (!$this.checkNotiryMessage(data)){
        // console.log('消息已经接收',data)
        return
      }

      if (data.eventType === 'CheckToken') {
        $this.MyRequest($this.ApiDoNameAuth+'User/info',{},function () {})
      }
      if (data.eventType === 'NORMAL') {
        $this.bus.$emit('NewSocketMessage', data)
      }
      if (data.eventType === 'NewOrder' || data.eventType === 'NewOrderApply') {
        $this.bus.$emit('NewOrderMessage', data)
        // 播放音乐
        $this.bus.$emit('PlayMusicCallBus',data.eventType.replace('New',''))
      }
    })

    $this.bus.$on('UserLoginCallBus',function () {
      if ($this.CurrUser.IsLogin()){
        $this.sendMessage({})
      }
    })

    $this.bus.$on('UserLoginOutCallBus',function () {
      $this.sendMessage({message:'LoginOut'})
    })
    // this.sockets.unsubscribe('newSocketIO-ServerMessage')
  },
  methods: {
    checkNotiryMessage:function (data) {
      if (data.eventType==='LOGIN'){
        return false
      }
      var obj=JSON.parse(data.message)
      var CurrMessageUUIDList=[]
      if (obj.messageUUID){
        var CurrMessageUUIDListString=localStorage.getItem("CurrMessageUUIDList")
        if (!CurrMessageUUIDListString){
          CurrMessageUUIDList=[]
        }else{
          CurrMessageUUIDList=JSON.parse(CurrMessageUUIDListString)
          if (CurrMessageUUIDList.indexOf(obj.messageUUID)>-1){
            return false
          }
        }
        CurrMessageUUIDList.push(obj.messageUUID)
        if (CurrMessageUUIDList.length>20){
          CurrMessageUUIDList.slice(0,5)
        }
        localStorage.setItem("CurrMessageUUIDList",JSON.stringify(CurrMessageUUIDList))
      }
      return true
    },
    sendMessage: function (messageParams) {
      var message = {
        messageType: 'text',
        message: '',
        token: 'Bearer '+this.CurrUser.Token,
        userType: 'Shop',
        userId: 0,
        deviceId: this.CurrUser.DeviceId,
        platform: this.getPlatform(),
        eventType: 'LOGIN',
        fromUserKey: '',
        toUserKey: '',
      }
      this.$socket.emit('newSocketIO-ClientMessage',Object.assign(message,messageParams))
    }
  }
}
</script>
